package redis

import (
	"context"
	red "github.com/go-redis/redis/v8"
	"time"
)

type (
	Redis struct {
		Host 	string
		DB   	int
		Auth 	string
		client 	*red.Client
	}
)


func New(host string, db int, auth string) *Redis {
	r := &Redis{
		Host: host,
		DB: db,
		Auth:  auth,
	}

	r.client = red.NewClient(r.redOptions())
	return r
}


func (r *Redis) redOptions() *red.Options {
	return &red.Options{
		Addr:               r.Host,
		DB:                 r.DB,
		DialTimeout:        10 * time.Second,
		ReadTimeout:        30 * time.Second,
		WriteTimeout:       30 * time.Second,
		PoolSize:           10,
		PoolTimeout:        30 * time.Second,
		IdleTimeout:        500 * time.Millisecond,
		IdleCheckFrequency: 500 * time.Millisecond,
	}
}

func (r *Redis) Publish(ctx context.Context, channel string, message interface{}) error {
	return r.client.Publish(ctx, channel, message).Err()
}